<script src="//maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=<?php echo $this->setting_model->item('googlemap_api') ?>"
type="text/javascript"></script>
<script type="text/javascript">
  var map, marker;
  var default_lat = <?php echo isset($obj['lat']) ? $obj['lat'] : '37.4419' ?>;
  var default_lng = <?php echo isset($obj['lng']) ? $obj['lng'] : '-122.1419' ?>;
  
  function create_marker(point){
    marker = new GMarker(point, {draggable: true});
    GEvent.addListener(marker, "dragstart", function() {
      //map.closeInfoWindow();
    });
    GEvent.addListener(marker, "dragend", function() {
      var point = marker.getPoint();
      map.panTo(point);
      jQuery("#lat").val( point.lat().toFixed(5) );
      jQuery("#lng").val( point.lng().toFixed(5) );
    });
    map.addOverlay(marker);
  }
  function initialize() {
    if (GBrowserIsCompatible()) {
      map = new GMap2(document.getElementById("map"));
      map.addControl(new GMapTypeControl());
      map.addControl(new GScaleControl());
      map.enableScrollWheelZoom();
      var customUI = map.getDefaultUI();
      customUI.maptypes.hybrid = false;
      map.setUI(customUI);
      var center = new GLatLng( default_lat, default_lng );
      map.setCenter(center, 13);
      create_marker(center);
    }
  }
  var country_id = 0, state_id = 0, city_id = 0;
  jQuery(document).ready(function(){
    initialize();    
    $('#country_id').bind({
      change: function (){detect_country($(this).val());},
      blur: function (){detect_country($(this).val());}
    });    
    $('#state_id').bind({
      change: function (){detect_state($(this).val());},
      blur: function (){detect_state($(this).val());}
    });
    $('#city_id').bind({
      change: function (){detect_city($(this).val());},
      blur: function (){detect_city($(this).val());}
    });
    detect_country($('#country_id').val());
  });
  function detect_city(id){
    if(city_id != id){
      city_id = id;
      if(id){
        $.post('<?php echo site_url('admin/ajax/get_json_city') ?>', {id : id}, function(data){
          map.clearOverlays();
          var center = new GLatLng( default_lat, default_lng );
          if(data){
            $('#lat').val(data.lat);
            $('#lng').val(data.lng);            
            center = new GLatLng( data.lat,data.lng);
          }
          map.setCenter(center);
          create_marker(center);
        }, 'json');
      }
    }
  }
  function detect_country(id){
    if(country_id != id){
      country_id = id;
      $('#state_id').html('');
      $('#state_id').html('<option value=""><?php echo $this->lang->line('label.state.select') ?></option>');
      if(id){
        $.post('<?php echo site_url('admin/ajax/list_json_state') ?>', {id : id}, function(data){
          if(data){                
            $.each(data, function(key, value){
              $('#state_id').append('<option '+
                (value['id'] == '<?php echo isset($obj['state_id']) ? $obj['state_id'] : 0 ?>' ? 'selected="selected"' :'' )
                +' value="'+value['id']+'">'+ value['name'] +'</option>');
              if(value['id'] == '<?php echo isset($obj['state_id']) ? $obj['state_id'] : 0 ?>'){
                detect_state(value['id']);
              }
            })
          }
        }, 'json');
      }
    }
  }
  function detect_state(id){
    if(state_id != id){
      state_id = id;
      $('#city_id').html('');
      $('#city_id').html('<option value=""><?php echo $this->lang->line('label.city.select') ?></option>');
      if(id){
        $.post('<?php echo site_url('admin/ajax/list_json_city') ?>', {id : id}, function(data){
          if(data){                
            $.each(data, function(key, value){
              $('#city_id').append('<option '+
                (value['id'] == '<?php echo isset($obj['city_id']) ? $obj['city_id'] : 0 ?>' ? 'selected="selected"' :'' )
                +' value="'+value['id']+'">'+ value['name'] +'</option>');
            })
          }
        }, 'json');
      }
    }
  }
  
  function add_image(event){
    var html = '<div style="float: left;width: 100%;padding-bottom: 5px;"><input type="file" name="images[]"/>&nbsp;&nbsp;<a href="#" onclick="$(this).parent().remove();return false;">remove</a></div>';
    $(event.target).parent().parent().append(html);
    return false;
  }
  
</script>

<link rel="stylesheet" href="<?php echo base_url() . APPPATH ?>themes/admin/css/jquery-ui-1.8.16.datepicker.css" type="text/css"/>
<script src="<?php echo base_url() . APPPATH ?>themes/admin/js/jui_datepicker.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
  jQuery(document).ready(function(){
    var dates = $( "#start_date, #end_date" ).datepicker({
      defaultDate: "now",
      dateFormat : "<?php echo $this->setting_model->item('js_date_format') ?>",
      changeMonth: true,
      onSelect: function( selectedDate ) {
        var option = this.id == "start_date" ? "minDate" : "maxDate",
        instance = $( this ).data( "datepicker" ),
        date = $.datepicker.parseDate(
        instance.settings.dateFormat ||
          $.datepicker._defaults.dateFormat,
        selectedDate, instance.settings );
        dates.not( this ).datepicker( "option", option, date );
      }
    });
    jQuery("#form_promotion").validationEngine();
  });
</script>